from code2_2 import *

class Water(Context):
    """水(H2O)"""

    def __init__(self):
        super().__init__()
        self.addState(SolidState("固态"))
        self.addState(LiquidState("液态"))
        self.addState(GaseousState("气态"))
        self.setTemperature(25)

    def getTemperature(self):
        return self._getStateInfo()

    def setTemperature(self, temperature):
        self._setStateInfo(temperature)

    def riseTemperature(self, step):
        self.setTemperature(self.getTemperature() + step)

    def reduceTemperature(self, step):
        self.setTemperature(self.getTemperature() - step)

    def behavior(self):
        state = self.getState()
        if (isinstance(state, State)):
            state.behavior(self)


# 单例的装饰器
def singleton(cls, *args, **kwargs):
    "构造一个单例的装饰器"
    instance = {}

    def __singleton(*args, **kwargs):
        if (cls not in instance):
            instance[cls] = cls(*args, **kwargs)
        return instance[cls]
    
    return __singleton


@singleton
class SolidState(State):
    """固态"""

    def __init__(self, name):
        super().__init__(name)

    def isMatch(self, stateInfo):
        return stateInfo < 0
    
    def behavior(self, context):
        print("我性格高冷，当前体温" + str(context._getStateInfo()) + "℃，我坚如钢铁，仿如一冷血动物，请用我砸人，嘿嘿......")


@singleton
class LiquidState(State):
    """液态"""

    def __init__(self, name):
        super().__init__(name)

    def isMatch(self, stateInfo):
        return (stateInfo >= 0 and stateInfo < 100)
    
    def behavior(self, context):
        print("我性格温和，当前体温" + str(context._getStateInfo()) + "℃，我可滋润万物，引用我可让你活力倍增......")


@singleton
class GaseousState(State):
    """气态"""

    def __init__(self, name):
        super().__init__(name)

    def isMatch(self, stateInfo):
        return stateInfo >= 100
    
    def behavior(self, context):
        print("我性格热烈，当前体温" + str(context._getStateInfo()) + "℃，飞向太空是我毕生的梦想，在这你将看不到我的存在，我将达到无我的境界......")



def testState():
    water = Water()
    water.behavior()
    water.setTemperature(-4)
    water.behavior()
    water.riseTemperature(18)
    water.behavior()
    water.riseTemperature(110)
    water.behavior()

testState()